// JavaScript Document
function viewPaper()
{
	with (window.document.frmListPaper) {
		if (cboCategory.selectedIndex == 0) {
			window.location.href = 'index.php';
		} else {
			window.location.href = 'index.php';
		}
	}
}

function checkAddPaperForm1()
{
	with (window.document.frmAddPaper) {
		/*if (cboStyle.selectedIndex == 0) {
			alert('Choose the Paper Group');
			cboStyle.focus();
			return;
		} else*/ if (isEmpty(txtMabb, 'Nhập mã bài báo')) {
			return;
		} else {
			submit();
		}
	}
}

function checkAddPaperForm(isAdd)
{
	with (window.document.frmAddPaper) {
		/*if (cboGroup.selectedIndex == 0) {
			alert('Choose the Topic Group');
			cboGroup.focus();
			return;
		} else*/ if (isEmpty(txtMabb, 'Nhập mã bài báo')) {
			return;
		} else {
			var table = document.getElementById('tbl_nth');
			var rows = table.getElementsByTagName('tr');
			var i, j, cells, str, str2;
			str = ''; // store list all newly added tac gia
			str2 = ''; // store list all selected tac gia id
			for (i = 1, j = rows.length; i < j; ++i) {
				cells = rows[i].getElementsByTagName('td');
				if (!cells.length) {
					continue;
				}
				// cells[6].innerHTML = 1 => new rows in modify mode
				if (isAdd || (cells[8].innerHTML == "1")) {
					//str = str + '(' + cells[0].innerHTML + ',{bb_id}' + '),';
					str = str + '(' + cells[0].innerHTML + ',{bb_id},' + i + '),';
					//alert (str);
				}
				str2 = str2 + cells[0].innerHTML + ',';
			}
			if (str2.length > 0) {
			    str2 = '(' + str2.substring(0, str2.length - 1) + ')';
			}
			if (str.length > 0) {
			   str = str.substring(0, str.length - 1);
			}
			
			// modify form has tg_sel input to store all selected tac gia id
			if (!isAdd)
			{
				$('#tg_sel').val(str2);
			}
			
			$('#tg').val(str);
			//alert("Hello " + str);
			
			if (!isAdd) {
				var strNoidung = '';
				$('#noidung_list .noidung_item').each(function(idx, item)
				{
					strNoidung += ',' + item.text;
				});
				if (strNoidung.length > 0)
				{
					strNoidung = strNoidung.substring(1);
				}
				$('#noidung_bb').val(strNoidung);
			}
			submit();
		}
	}
}
function uploadFile()
{
	var ajax_url = "multiple_upload_ac.php";
	$('#form1').post(ajax_url, 
		function(data) {
			$('#uploaded').html(data);
		});
}

function addPaper()
{
	window.location.href = 'index.php?view=add';
}

function modifyPaper(id)
{
	window.location.href = 'index.php?view=modify&id=' + id;
}

function deletePaper(id)
{
	if (confirm('Xóa bài báo này?')) {
		window.location.href = 'processPaper.php?action=deletePaper&id=' + id;
	}
}
var gCount = 1;
function appendRow()
{
 //alert("AAAAAAAAAAa");
 if  ($('#cboAuthor-Topic option:selected').val() == 0) return;
 nth = $('#cboAuthor-Topic option:selected').text();
 //alert(nth);
 donvi = $('#cboDept option:selected').text();
 truong = $('#cboSchool option:selected').text();
 var tbody = document.getElementById("tbl_nth").getElementsByTagName("tbody")[0];
 var row = document.createElement("TR");
 var cell_idnth = document.createElement("TD");
 cell_idnth.innerHTML = $('#cboAuthor-Topic option:selected').val();
 cell_idnth.style.display = "none";
 var cell_iddv = document.createElement("TD");
 cell_iddv.innerHTML = $('#cboDept option:selected').val();
 cell_iddv.style.display = "none";
 var cell_idtrg = document.createElement("TD");
 cell_idtrg.innerHTML = $('#cboSchool option:selected').val();
 cell_idtrg.style.display = "none";
 var cell1 = document.createElement("TD");
 cell1.innerHTML = nth;
 var cell2 = document.createElement("TD");
 cell2.innerHTML = donvi;
 
 var celltrg = document.createElement("TD");
 celltrg.innerHTML = truong;

var cell3 = document.createElement("TD");
 var tbl = document.getElementById('tbl_nth');
 var count = tbl.rows.length;
 cell3.innerHTML = "<a href=\"javascript:deleteRow(" + gCount + ")\">X</a>";
 var cell4 = document.createElement("TD");
 cell4.style.display = "none";
 var tbl = document.getElementById('tbl_nth');
 var count = tbl.rows.length;
 cell4.innerHTML = gCount;
 
 var cell_status = document.createElement("TD");
 cell_status.innerHTML = "1"; // newly added
 cell_status.style.display = "none";
 
 row.appendChild(cell_idnth);
 row.appendChild(cell_iddv);
 row.appendChild(cell_idtrg);
 row.appendChild(cell1);
 row.appendChild(cell2);
 row.appendChild(celltrg);
 row.appendChild(cell3);
 row.appendChild(cell4);
 row.appendChild(cell_status);

 tbody.appendChild(row);
 gCount = gCount + 1;
}

function deleteRow(index)
{
	var tbl = document.getElementById('tbl_nth');
	
	var rows = tbl.getElementsByTagName('tr');
	var i, j, cells, str;
	for (i = 0; i < rows.length; i++) {
		cells = rows[i].getElementsByTagName('td');
		if (!cells.length) {
			continue;
		}
		//alert (cells.length);
		if (cells[7].innerHTML == index) {
			tbl.deleteRow(i);
		}
	}
}
function deleteRows() {     
	var tbl = document.getElementById('tbl_nth'), 
	// table reference         
	lastRow = tbl.rows.length - 1,             
	// set the last row index         
	i;     
	// delete rows with index greater then 0     
	for (i = lastRow; i > 0; i--) {         
		tbl.deleteRow(i);    
		} 
} 